<template>
	<view class="products">
		<view class="search-bar">
			<view class="item">
				<text v-if="searchType==0">交易日：</text>
				<text v-else-if="searchType == 1">交易月：</text>
				<text v-else-if="searchType == 2">交易年：</text>
				<text>{{ date }}</text>
			</view>
			<view class="item">
				商品名称：{{ productName }}
			</view>
		</view>
		<view class="table-wrapper">
			<view class="thead-wrapper">
				<view class="th center" style="width: 100upx;">
					名次
				</view>
				<view class="th flex">
					<text>客户名称</text>
				</view>
				<view class="th" style="width: 150upx;" @click="beSort('salesWeight')">
					<text>售后重量</text>
					<image v-if="sort === 'salesWeight'" src="../static/image/sort.png" mode="aspectFill" class="icon">
					</image>
				</view>
				<view class="th" style="width: 150upx;" @click="beSort('refundAmount')">
					<text>退款金额</text>
					<image v-if="sort === 'refundAmount'" src="../static/image/sort.png" mode="aspectFill" class="icon">
					</image>
				</view>
				<view class="th" style="width: 120upx;" @click="beSort('defectiveRate')">
					<text>不良率</text>
					<image v-if="sort === 'defectiveRate'" src="../static/image/sort.png" mode="aspectFill"
						class="icon">
					</image>
				</view>
			</view>
			<view class="tbody-wrapper" v-if="list.length">
				<view class="item" v-for="(item,index) in list" :key="index">
					<view class="tr">
						<view class="td center">
							{{ index + 1 }}、
						</view>
						<view class="td flex">
							<view class="productName">
								{{ item.customerPetName }}
							</view>
						</view>
						<view class="td" style="width: 150upx;text-align: center;">
							{{ item.salesWeight }}斤
						</view>
						<view class="td" style="width: 150upx;text-align: center;">
							{{ item.refundAmount }}元
						</view>
						<view class="td" style="width: 120upx;text-align: center;">
							{{ parseFloat((item.defectiveRate * 100).toFixed(2)) }}%
						</view>
					</view>
				</view>
			</view>

			<view class="nodata-wrapper" v-else>
				<image src="https://sm.hobon.cc/static/hobon_noorder.png" class="data-image" mode="widthFix"></image>
				<view class="text">
					暂无数据~
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// import {
	// 	tableToExcel
	// } from '@/pages/util/exportExcel.js'

	export default {
		data() {
			return {
				productName: '',
				productId: '',
				searchType: 0, //0: 日  1: 月  2: 年
				date: '',
				list: [],
				sort: 'salesWeight', // 排序：销量salesVolume   销售额salesAmount 
			}
		},
		computed: {
			userInfo() {
				return this.$store.state.userInfo
			}
		},
		onLoad(option) {
			console.log("option", option)
			this.productName = option.name;
			this.productId = option.id;
			this.date = option.date;
			this.searchType = option.searchType;
			this.getData()
		},
		onNavigationBarButtonTap() {
			this.exportExcel()
		},
		methods: {
			async getData() {
				const params = {
					pickupPointId: this.userInfo.pickupPointId,
					productId: this.productId
				}
				if (this.searchType == 0) {
					params.queryDay = this.date;
				} else if (this.searchType == 1) {
					params.queryMonth = this.date;
				} else if (this.searchType == 2) {
					params.queryYear = this.date;
				}
				const data = await this.$api.post('/app/statistic/pickupPointAfterSalesDetail', params)
				this.list = [];
				if (data.code === 200) {
					this.list = data.data;
					this.beSort('salesWeight')
				}
			},
			selectDate(type) {
				if (this.searchType === type) return
				this.searchType = type;
				if (type === 0) {
					this.startDate = this.$helper.getDay(0, '-')
				} else if (type === 1) {
					this.startDate = this.$helper.getDay(0, '-').substring(0, 7)
				} else if (type === 2) {
					this.startDate = this.$helper.getDay(0, '-').substring(0, 4)
				}
				this.getData()
			},
			changeDate(e) {
				this.startDate = e.detail.value;
				this.getData()
			},
			beSort(column) {
				this.sort = column;
				this.list.sort((a, b) => {
					return b[column] - a[column]
				})
			},
			exportExcel() {
				if (!this.list.length) {
					uni.showToast({
						title: '暂无可导出数据',
						icon: 'none'
					})
					return
				}

				//要导出的json数据
				const tHead = ['序号', '货品名称', '规格', '采购量', '营业额贡献(元)']
				const jsonData = this.list.map((item, index) => {
					return {
						index: (index + 1),
						productName: item.productName,
						productSpecName: (item.productSpecName || '默认规格') + (
							item.productSpecPackageManner ? ('(' + item.productSpecQuantity + item
								.productSpecPackageManner +
								'/' + item.productSpecPackageUnit + ')') : '(散装)'
						),
						productQuantity: item.productQuantity,
						productAmount: item.productAmount
					}
				})

				let title = '商品采购报表';
				if (this.searchType === 1) {
					title = `${this.startDate.split('-')[0]}年${this.startDate.split('-')[1]}月-商品采购报表`
				} else if (this.searchType === 0) {
					title = `${this.startDate}-商品采购报表`
				} else {
					title = `${this.startDate}至${this.endDate}-商品采购报表`
				}
				//	tableToExcel(tHead, jsonData, title)
			},
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.products {
		height: 100%;
		overflow: auto;

		.search-bar {
			background-color: #ffffff;
			margin-top: 2upx;
			padding: 20upx;
			margin-bottom: 2rpx;

			.item {
				font-size: 34rpx;
				color: #222222;

				&:first-child {
					margin-bottom: 10rpx;
				}
			}
		}

		.tip {
			padding-left: 30rpx;
			height: 60rpx;
			display: flex;
			align-items: center;
			font-size: 28upx;
			color: #666666;
		}

		.table-wrapper {
			.thead-wrapper {
				height: 88upx;
				display: flex;
				align-items: center;
				background-color: #ffffff;
				font-size: 26upx;

				.th {
					&.center {
						text-align: center;
					}

					&.flex {
						flex: 1;
					}

					.icon {
						width: 24upx;
						height: 24rpx;
					}
				}
			}

			.tbody-wrapper {
				.item {
					//min-height: 126upx;
					background-color: #ffffff;
					font-size: 26upx;
					margin: 10rpx 10rpx 0 10rpx;
					border-radius: 10rpx;
					padding: 20rpx 20rpx;
					box-sizing: border-box;
				}

				.tr {
					display: flex;
					align-items: center;

					&:first-child {
						margin-bottom: 10rpx;
					}
				}

				.td {
					&.center {
						text-align: center;
					}

					&.flex {
						flex: 1;
					}

					.icon {
						width: 24upx;
						height: 26rpx;
					}

					.productName {
						font-size: 26upx;
						color: #292929;
						line-height: 34upx;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden; //溢出内容隐藏
						text-overflow: ellipsis; //文本溢出部分用省略号表示
						display: -webkit-box; //特别显示模式
						-webkit-line-clamp: 2; //行数
						line-clamp: 2;
						-webkit-box-orient: vertical; //盒子中内容竖直排列
						width: 90%;
						padding-right: 30upx;
					}

					.productSpecName {
						color: #898989;
						font-size: 28upx;
					}
				}
			}
		}

		.nodata-wrapper {
			text-align: center;
			font-size: 32upx;
			color: #666666;

			.data-image {
				margin-top: 200upx;
				margin-bottom: 10upx;
				width: 296upx;
				height: 205upx;
			}
		}
	}
</style>